package com.wqr.mobile.android.ui.task;

import android.os.AsyncTask;
import android.util.Log;

/**
 * 
 * @author wenqiurong
 * 
 */
public class HttpRequestTask extends AsyncTask<Object, Integer, HttpTaskResult> {
	private static final String TAG = HttpRequestTask.class.getSimpleName();
	HttpTaskCallBack mListener;
	public boolean isFinished = false;

	public HttpRequestTask(HttpTaskCallBack listener) {
		this.mListener = listener;
	}

	@Override
	protected HttpTaskResult doInBackground(Object... params) {
		return mListener.onTaskRunning(params);
	}

	@Override
	protected void onCancelled() {
		this.cancel(true);
		mListener.onCancelled(this.getStatus());
		Log.i(TAG, "task onCancelled,status=" + this.getStatus());
		super.onCancelled();
	}

	@Override
	protected void onPostExecute(HttpTaskResult result) {
		isFinished = true;
		mListener.onTaskFinished(result);
		super.onPostExecute(result);
	}

	@Override
	protected void onPreExecute() {
		mListener.onTaskStart();
		super.onPreExecute();
	}

	@Override
	protected void onProgressUpdate(Integer... values) {
		super.onProgressUpdate(values);
	}

	/**
	 * 
	 * @author wenqiurong
	 * 
	 */
	public interface HttpTaskCallBack {

		/**
		 * on Task Start
		 */
		public void onTaskStart();

		/**
		 * on Task Running
		 */
		public HttpTaskResult onTaskRunning(Object... params);

		/**
		 * onCancelled
		 */
		public void onCancelled(Status status);

		/**
		 * on Task Finished
		 */
		public void onTaskFinished(HttpTaskResult result);

	}
}
